package cn.gemiman.daoyi.module.cms.controller.admin.blogmessage;

import cn.gemiman.daoyi.framework.common.enums.UserTypeEnum;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.gemiman.daoyi.framework.common.pojo.PageParam;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;

import cn.gemiman.daoyi.framework.excel.core.util.ExcelUtils;

import cn.gemiman.daoyi.framework.apilog.core.annotation.ApiAccessLog;
import static cn.gemiman.daoyi.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.gemiman.daoyi.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.gemiman.daoyi.module.cms.controller.admin.blogmessage.vo.*;
import cn.gemiman.daoyi.module.cms.dal.dataobject.blogmessage.BlogMessageDO;
import cn.gemiman.daoyi.module.cms.service.blogmessage.BlogMessageService;

@Tag(name = "管理后台 - 留言")
@RestController
@RequestMapping("/cms/blog-message")
@Validated
public class BlogMessageController {

    @Resource
    private BlogMessageService blogMessageService;

    @PostMapping("/create")
    @Operation(summary = "创建留言")
    @PreAuthorize("@ss.hasPermission('cms:blog-message:create')")
    public CommonResult<Long> createBlogMessage(@Valid @RequestBody BlogMessageSaveReqVO createReqVO) {
        createReqVO.setUserId(getLoginUserId()).setUserType(UserTypeEnum.ADMIN.getValue());
        return success(blogMessageService.createBlogMessage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新留言")
    @PreAuthorize("@ss.hasPermission('cms:blog-message:update')")
    public CommonResult<Boolean> updateBlogMessage(@Valid @RequestBody BlogMessageSaveReqVO updateReqVO) {
        blogMessageService.updateBlogMessage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除留言")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:blog-message:delete')")
    public CommonResult<Boolean> deleteBlogMessage(@RequestParam("id") Long id) {
        blogMessageService.deleteBlogMessage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得留言")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:blog-message:query')")
    public CommonResult<BlogMessageRespVO> getBlogMessage(@RequestParam("id") Long id) {
        BlogMessageDO blogMessage = blogMessageService.getBlogMessage(id);
        return success(BeanUtils.toBean(blogMessage, BlogMessageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得留言分页")
    @PreAuthorize("@ss.hasPermission('cms:blog-message:query')")
    public CommonResult<PageResult<BlogMessageRespVO>> getBlogMessagePage(@Valid BlogMessagePageReqVO pageReqVO) {
        PageResult<BlogMessageDO> pageResult = blogMessageService.getBlogMessagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BlogMessageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出留言 Excel")
    @PreAuthorize("@ss.hasPermission('cms:blog-message:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBlogMessageExcel(@Valid BlogMessagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BlogMessageDO> list = blogMessageService.getBlogMessagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "留言.xls", "数据", BlogMessageRespVO.class,
                        BeanUtils.toBean(list, BlogMessageRespVO.class));
    }

}
